package thread

type Thread struct {
	pc    int
	stack *Stack
}

func NewThread() *Thread {
	return &Thread{stack: NewStack(1024)}
}

func NewStack(capacity uint64) *Stack {
	return &Stack{
		Capacity: capacity,
	}
}

func (self *Thread) PC() int {
	return self.pc
}

func (self *Thread) SetPC(pc int) {
	self.pc = pc
}

func (self *Thread) PushFrame(frame *Frame) {

}

func (self *Thread) PopFrame() *Frame {
	return nil
}
func (self *Thread) CurFrame() *Frame {
	return nil
}

type Stack struct {
	Capacity uint64
	Len      uint64
	Top      *Frame
}

func (self Stack) PushFrame(frame *Frame) {
	if self.Len >= self.Capacity {
		panic("java.lang.StackOverflowError")
	}
	if self.Top != nil {
		frame.before = self.Top
	}
	self.Top = frame
	self.Len++
}

func (self Stack) PopFrame() *Frame {
	if self.Top == nil {
		panic("JVM STACK IS EMPTY!")
	}
	top := self.Top
	self.Top = top.before
	top.before = nil
	return top
}
